package cn.edu.zjicm.wordsnet_d.BroadcastReceiver;

import cn.edu.zjicm.wordsnet_d.activity.ExamRunActivity;
import cn.edu.zjicm.wordsnet_d.activity.LockActivity;
import cn.edu.zjicm.wordsnet_d.activity.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
	static final String SYSTEM_REASON = "reason";
	static final String SYSTEM_HOME_KEY = "homekey";// home key
	static final String SYSTEM_LONG_HOME_KEY = "globalactions"; // long home key

	Context context1;
	Context context2;
	Context context3;

	@Override
	public void onReceive(Context context, Intent intent) {
		context1 = MainActivity.getContext();
		context2 = LockActivity.getContext();
		context3 = ExamRunActivity.getContext();
		String action = intent.getAction();
		if (action != null) {
			if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
				String reason = intent.getStringExtra(SYSTEM_REASON);
				if (reason != null) {
					Log.e("HomeKeyEventBroadCastReceiver", reason);
					// Log.d("Tag",
					// "HomeKeyEventBroadCastReceiver里的reason="+reason+",context="+context);
					if (reason.equals(SYSTEM_HOME_KEY)) {
						// home key处理点
						// if (context.equals(context1))
						// AlarmReceiver.alarmCheckStudy(context);
						if (context2 != null && context2.equals(context)) {
							// Log.d("Tag", "对锁屏处理--------");
							((LockActivity) LockActivity.mContext).finishMyActivity();
						} else if (context3 != null && context3.equals(context)) {
							// Log.d("Tag", "对学习界面处理--------");
							((ExamRunActivity) ExamRunActivity.mContext).homeFlag = true;
							((ExamRunActivity) ExamRunActivity.mContext).pauseTimer();
						}
					} else if (reason.equals(SYSTEM_LONG_HOME_KEY)) {
						if (context3 != null && context3.equals(context)) {
							((ExamRunActivity) ExamRunActivity.mContext).homeFlag = true;
							((ExamRunActivity) ExamRunActivity.mContext).pauseTimer();
						}
					}
				}
			}
		}
	}

	/*
	 * public static HomeKeyEventBroadCastReceiver receiver;
	 * 
	 * public static void registerHomeKeyEventBroadCastReceiver(Context context)
	 * { Log.e("HomeKeyEventBroadCastReceiver","register");
	 * AlarmReceiver.cancleNotifyStudy(context); if (receiver == null) {
	 * receiver = new HomeKeyEventBroadCastReceiver();
	 * context.registerReceiver(receiver, new IntentFilter(
	 * Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); } }
	 * 
	 * public static void unregisterHomeKeyEventBroadCastReceiver(Context
	 * context) { Log.e("HomeKeyEventBroadCastReceiver","unregister"); if
	 * (receiver != null) { context.unregisterReceiver(receiver); receiver =
	 * null; } }
	 */

}
